<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\menusRepositories;
use App\Repositories\PermissionsRepositories;
use App\Repositories\RolesRepositories;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MenusController extends Controller
{
    public function __construct(
        menusRepositories $menusRepositories,
        PermissionsRepositories $permissionsRepositories,
        RolesRepositories $rolesRepositories
    )
    {
        $this->menusRepositories = $menusRepositories;
        $this->permissionsRepositories = $permissionsRepositories;
        $this->rolesRepositories = $rolesRepositories;

    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //dd(admin_menu());
        //echo  request()->getSchemeAndHttpHost();
        return view('admin.menus.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $permissions =$this->permissionsRepositories->all();
        $roles =$this->rolesRepositories->all();
        $menus =$this->menusRepositories->all();

       // echo \Request::getRequestUri();

       // echo \Request::route()->getName();

        return view('admin.menus.create',compact('roles','permissions','menus'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'title'  => 'required|string',

        ],[
            'title.required'=>'* 名称必传!',

        ]);
        $ret = $this->menusRepositories->save($request->all());

        if($ret)
        {
            return redirect()->route('admin.menus')->with(['success'=>'添加完成']);
        }
        return redirect()->back()->with(['success'=>'系统错误']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id,Request $request)
    {
        $info = $this->menusRepositories->find($id);
        //$permissions =$this->permissionsRepositories->all();
        $roles =$this->rolesRepositories->all();
        $menus =$this->menusRepositories->all();

        $checkArrR = $checkArrP = [];
        /*foreach ($info->manyPermissions as $key =>$val)
        {
            $checkArrP[] = $val->id;
        }*/
        foreach ($info->manyRoles as $key =>$val)
        {
            $checkArrR[] = $val->id;
        }
        return view('admin.menus.edit',compact('info','roles','permissions','checkArrR','checkArrP','menus'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $this->validate($request,[
            'title'  => 'required|string',
            'uri'  => 'required|string',
        ],[
            'title.required'=>'* 标题必传!',
            'uri.required'=>'*路径必传!',
        ]);
        $ret = $this->menusRepositories->update($id,$request->all());

        if($ret)
        {
            return redirect()->route('admin.menus')->with(['success'=>'更新成功！']);
        }
        return redirect()->back()->with(['success'=>'系统错误']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
        $ids=request()->input('ids');

        if(empty($ids))
        {
            return response()->json([
                'code' => 0,
                'msg'   => '请选择删除项！',
            ]);
        }

        if(!is_array($ids))
        {
            return response()->json([
                'code' => 0,
                'msg'   => '非法参数！',
            ]);
        }

        $ret =$this->usersRepositories->delete($ids);
        if($ret)
        {
            return response()->json([
                'code' => 0,
                'msg'   => '删除成功！',
            ]);
        }
        return response()->json([
            'code' => -1,
            'msg'   => '删除失败！',
        ]);
    }
    public function data(Request $request)
    {
        $res = $this->menusRepositories->all();
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => 0,
            'data'  => $res
        ];
        return response()->json($data);
    }
}
